home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Sample 2.4 Think C distribution / utl.c < prev    next >
Text File  |  1991-02-25  |  59KB  |  1,951 lines

  1. /*______________________________________________________________________
  2.  
  3.     utl.c - Utilities.
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is granted
  6.     to use this code in your own projects, provided you give credit to both
  7.     John Norstad and Northwestern University in your about box or document.
  8.     
  9.     This module exports miscellaneous reusable utility routines.
  10. _____________________________________________________________________*/
  11.  
  12. #include    <string.h>
  13. #include <color.h>
  14. #include "utl.h"
  15.  
  16. #define nil 0
  17. #define _Unimplemented        0xA89F
  18. #define _PopUpMenuSelect    0xA80B
  19. #define _WaitNextEvent        0xA860
  20.  
  21. /*______________________________________________________________________
  22.  
  23.     Global Variables.
  24. _____________________________________________________________________*/
  25.  
  26.  
  27. static SysEnvRec        TheWorld;        /* system environment record */
  28. static Boolean            GotSysEnviron = false;    
  29.                                                 /* true if sys environ has been
  30.                                                     gotten */
  31.     
  32.             
  33. static CursHandle     *CursArray;        /* ptr to array of cursor handles */
  34. static short             NumCurs;            /* number of cursors to rotate */
  35. static short             TickInterval;    /* number of ticks between rotations */
  36. static short             CurCurs;            /* index of current cursor */
  37. static short             LastTick;        /* tick count at loast rotation */
  38.  
  39. static ModalFilterProcPtr    Filter;    /* dialog filter proc */
  40. static short            CancelItem;        /* item number of cancel button */
  41.  
  42. /*______________________________________________________________________
  43.  
  44.     GetSysEnvirons - Get System Environment.
  45.     
  46.     Exit:        global variable TheWorld = system environment record.
  47.                 global variable GotSysEnviron = true.
  48. _____________________________________________________________________*/
  49.  
  50.  
  51. static void GetSysEnvirons (void)
  52.  
  53. {
  54.     if (!GotSysEnviron) {
  55.         (void) SysEnvirons(curSysEnvVers, &TheWorld);
  56.         GotSysEnviron = true;
  57.     };
  58. }
  59.  
  60. /*______________________________________________________________________
  61.  
  62.     utl_AppendDITL - Append DITL to End of Dialog.
  63.     
  64.     Entry:    theDialog = pointer to dialog.
  65.                 theDITLID = rsrc id of DITL.
  66.                     
  67.     Exit:        function result = item number of first appended item.
  68.     
  69.     The dialog window is expanded to accomodate the new items, and the
  70.     new items are offset to appear at the bottom of the dialog.
  71.     
  72.     This routine is particularly useful for appending items to the
  73.     standard Page Setup and Print Job dialogs.  (See TN 95).  It was
  74.     written by Lew Rollins of Apple's Human-Systems Interface Group,
  75.     in MPW Pascal.  I translated it to MPW C.
  76.     
  77.     The only significant difference between this routine and Rollin's
  78.     version is that this version does not release the DITL resource.
  79. _____________________________________________________________________*/
  80.  
  81.  
  82. short utl_AppendDITL (DialogPtr theDialog, short theDITLID)
  83.  
  84. {
  85.     typedef struct DITLItem {
  86.         Handle            itmHndl;                /* handle or proc ptr */
  87.         Rect                itmRect;                /* display rect */
  88.         char                itmType;                /* item type */
  89.         unsigned char    itmData;                /* item data length byte */
  90.     } DITLItem;
  91.     
  92.     typedef struct itemList {
  93.         short                dlgMaxIndex;        /* num items - 1 */
  94.         DITLItem            DITLItems[1];        /* array of DITL items */
  95.     } itemList;
  96.     
  97.     short                offset;            /* item offset */
  98.     Rect                maxRect;            /* max dialog rect size so far */
  99.     Handle            hDITL;            /* handle to DITL */
  100.     DITLItem            *pItem;            /* pointer to item being appended */
  101.     itemList            **hItems;        /* handle to DLOG's item list */
  102.     short                sizeDITL;        /* size of DLOG's item list */
  103.     short                firstItem;        /* item num of first appended item */
  104.     short                newItems;        /* number of new items */
  105.     short                dataSize;        /* size of data for current item */
  106.     short                i;                    /* loop index */
  107.     
  108.     /* Initialize. */
  109.     
  110.     maxRect = theDialog->portRect;
  111.     offset = maxRect.bottom;
  112.     maxRect.bottom -= 5;
  113.     maxRect.right -= 5;
  114.     hItems = (itemList**)(((DialogPeek)theDialog)->items);
  115.     sizeDITL = GetHandleSize((Handle)hItems);
  116.     firstItem = (**hItems).dlgMaxIndex + 2;
  117.     hDITL = GetResource('DITL', theDITLID);
  118.     HLock(hDITL);
  119.     newItems = **(short**)hDITL + 1;
  120.     PtrAndHand(*hDITL+2, (Handle)hItems,
  121.         GetHandleSize(hDITL)-2);
  122.     (**hItems).dlgMaxIndex += newItems;
  123.     HUnlock(hDITL);
  124.     HLock((Handle)hItems);
  125.     pItem = (DITLItem *)((char*)(*hItems) + sizeDITL);
  126.     
  127.     /* Main loop.  Add each item to dialog item list. */
  128.     
  129.     for (i = 1; i <= newItems; i++) {
  130.         OffsetRect(&pItem->itmRect, 0, offset);
  131.         UnionRect(&pItem->itmRect, &maxRect, &maxRect);
  132.         switch (pItem->itmType & 0x7f) {
  133.             case ctrlItem+btnCtrl:
  134.             case ctrlItem+chkCtrl:
  135.             case ctrlItem+radCtrl:
  136.                 pItem->itmHndl = (Handle)NewControl(theDialog, 
  137.                     &pItem->itmRect, &pItem->itmData, true, 0, 0, 1, 
  138.                     pItem->itmType & 0x03, 0);
  139.                 break;
  140.             case ctrlItem+resCtrl:
  141.                 pItem->itmHndl = (Handle)GetNewControl(
  142.                     *(short*)(&pItem->itmData+1),
  143.                     theDialog);
  144.                 (**((ControlHandle)(pItem->itmHndl))).contrlRect = 
  145.                     pItem->itmRect;
  146.                 break;
  147.             case statText:
  148.             case editText:
  149.                 PtrToHand(&pItem->itmData+1, (Handle *)&(pItem->itmHndl), 
  150.                     pItem->itmData);
  151.                 break;
  152.             case iconItem:
  153.                 pItem->itmHndl = GetIcon(*(short*)(&pItem->itmData+1));
  154.                 break;
  155.             default:
  156.                 pItem->itmHndl = nil;
  157.         };
  158.         dataSize = (pItem->itmData + 1) & 0xfffe;
  159.         pItem = (DITLItem *)((char*)pItem + dataSize + sizeof(DITLItem));
  160.     };
  161.     
  162.     /* Finish up. */
  163.     
  164.     HUnlock((Handle)hItems);
  165.     maxRect.bottom += 5;
  166.     maxRect.right += 5;
  167.     SizeWindow(theDialog, maxRect.right, maxRect.bottom, true);
  168.     return firstItem;
  169. }
  170.  
  171. /*______________________________________________________________________
  172.  
  173.     utl_CenterDlogRect - Center a dialog rectangle.
  174.     
  175.     Entry:    rect = rectangle.
  176.                 centerMain = true to center on main (menu bar) screen.
  177.                 centerMain = false to center on the screen containing
  178.                     the maximum intersection with the frontmost window.
  179.     
  180.     Exit:        rect = rectangle offset so that it is centered on
  181.                     the specified screen, with twice as much space below
  182.                     the rect as above.
  183.                     
  184.     See HIN 6.
  185. _____________________________________________________________________*/
  186.  
  187.  
  188. void utl_CenterDlogRect (Rect *rect, Boolean centerMain)
  189.  
  190. {
  191.     Rect        screenRect;            /* screen rectangle */
  192.     short        mBHeight;            /* menu bar height */
  193.     GDHandle    gd;                    /* gdevice */
  194.     Rect        windRect;            /* window rectangle */
  195.     Boolean    hasMB;                /* true if screen contains menu bar */
  196.  
  197.     mBHeight = utl_GetMBarHeight();
  198.     if (centerMain) {
  199.         screenRect = screenBits.bounds;
  200.     } else {
  201.         utl_GetWindGD(FrontWindow(), &gd, &screenRect, &windRect, &hasMB);
  202.         if (!hasMB) mBHeight = 0;
  203.     };
  204.     OffsetRect(rect,
  205.         (screenRect.right + screenRect.left - rect->right - rect->left) >> 1,
  206.         (screenRect.bottom + ((screenRect.top + mBHeight - rect->top)<<1) - 
  207.             rect->bottom + 7) / 3);
  208. }
  209.  
  210. /*______________________________________________________________________
  211.  
  212.     utl_CenterRect - Center a rectangle on the main screen.
  213.     
  214.     Entry:    rect = rectangle.
  215.     
  216.     Exit:        rect = rectangle offset so that it is centered on
  217.                     the main screen.
  218. _____________________________________________________________________*/
  219.  
  220.  
  221. void utl_CenterRect (Rect *rect)
  222.  
  223. {
  224.     Rect        screenRect;            /* main screen rectangle */
  225.     short        mBHeight;            /* menu bar height */
  226.  
  227.     mBHeight = utl_GetMBarHeight();
  228.     screenRect = screenBits.bounds;
  229.     OffsetRect(rect,
  230.         (screenRect.right + screenRect.left - rect->right - rect->left) >> 1,
  231.         (screenRect.bottom - screenRect.top + mBHeight - 
  232.             rect->bottom - rect->top) >> 1);
  233. }
  234.  
  235. /*______________________________________________________________________
  236.  
  237.     utl_CheckPack - Check to see if a package exists.
  238.     
  239.     Entry:    packNum = package number.
  240.                 preload = true to preload package.
  241.                     
  242.     Exit:        function result = true if package exists.
  243. _____________________________________________________________________*/
  244.  
  245.  
  246. Boolean utl_CheckPack (short packNum, Boolean preload)
  247.  
  248. {
  249.     short            trapNum;            /* trap number */
  250.     Handle        h;                    /* handle to PACK resource */
  251.     
  252.     /* Check to make sure the trap exists, by comparing its trap address to
  253.         the trap address of the unimplemented trap. */
  254.     
  255.     trapNum = packNum + 0x1e7;
  256.     if (NGetTrapAddress(trapNum & 0x3ff, ToolTrap) == 
  257.         NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap)) return false;
  258.         
  259.     /* Check to make sure the package exists on the System file or in 
  260.         ROM.  If it's not in ROM make it nonpurgeable, if requested. */
  261.     
  262.     if (preload) {
  263.         if (utl_Rom64()) {
  264.             h = GetResource('PACK', packNum);
  265.             if (!h) return false;
  266.             HNoPurge(h);
  267.         } else {
  268.             RomMapInsert = 0xFF00;
  269.             h = GetResource('PACK', packNum);
  270.             if (!h) return false;
  271.             if ((*(unsigned long*)h & 0x00FFFFFF) < 
  272.                 (long)ROMBase) {
  273.                 h = GetResource('PACK', packNum);
  274.                 HNoPurge(h);
  275.             };
  276.         };
  277.         return true;
  278.     } else {
  279.         SetResLoad(false);
  280.         if (!utl_Rom64()) RomMapInsert = 0xFF00;
  281.         h = GetResource('PACK', packNum);
  282.         SetResLoad(true);
  283.         if (h) return true; else return false;
  284.     };
  285. }
  286.  
  287. /*______________________________________________________________________
  288.  
  289.     utl_CopyPString - Copy Pascal String.
  290.     
  291.     Entry:    dest = destination string.
  292.                 source = source string.
  293. _____________________________________________________________________*/
  294.  
  295.  
  296. void utl_CopyPString (Str255 *dest, Str255 *source)
  297.  
  298. {
  299.     memcpy(dest, source, (*source)[0]+1);
  300. }
  301.  
  302. /*______________________________________________________________________
  303.  
  304.     utl_CouldDrag - Determine if a window could be dragged to a location.
  305.     
  306.     Entry:    windRect = window rectangle, in global coords.
  307.                 offset = pixel offset used in DragRect calls.
  308.     
  309.     Exit:        function result = true if the window could have been
  310.                     dragged to the specified position.
  311.                     
  312.     This routine is used when restoring windows to saved positions.  According
  313.     to HIN 6, we must check to see if the window "could have been dragged to
  314.     the saved position."
  315.     
  316.     The "offset" parameter is usually 4.  When initializing the boundary rectangle
  317.     for DragWindow calls, normally the boundary rectangle of the desktop gray
  318.     region is inset by 4 pixels.  If some value other than 4 is used, it should
  319.     be passed to CouldDrag as the "offset" parameter.
  320.     
  321.     The algorithm used is the following:  The routine computes the four squares
  322.     at the corners of the title bar.  "true" is returned if and only if at least one 
  323.     of these four squares is completely contained within the desktop gray region.
  324.     
  325.     Three pixels are added to the offset to err on the side of requiring a larger
  326.     portion of the drag bar to be visible.  
  327. _____________________________________________________________________*/
  328.  
  329.  
  330. Boolean utl_CouldDrag (Rect *windRect, short offset)
  331.  
  332. {
  333.     RgnHandle            rgn;            /* scratch region handle */
  334.     Boolean                could;        /* function result */
  335.     short                    corner;        /* which corner */
  336.     Rect                    r;                /* corner rectangle */
  337.  
  338.     rgn = NewRgn();
  339.     could = false;
  340.     offset += 3;
  341.     for (corner = 1; corner <= 4; corner++) {
  342.         switch (corner) {
  343.             case 1:
  344.                 r.top = windRect->top - titleBarHeight;
  345.                 r.left = windRect->left;
  346.                 break;
  347.             case 2:
  348.                 r.top = windRect->top - offset;
  349.                 r.left = windRect->left;
  350.                 break;
  351.             case 3:
  352.                 r.top = windRect->top - titleBarHeight;
  353.                 r.left = windRect->right - offset;
  354.                 break;
  355.             case 4:
  356.                 r.top = windRect->top - offset;
  357.                 r.left = windRect->right - offset;
  358.                 break;
  359.         };
  360.         r.bottom = r.top + offset;
  361.         r.right = r.left + offset;
  362.         RectRgn(rgn, &r);
  363.         DiffRgn(rgn, GrayRgn, rgn);
  364.         if (EmptyRgn(rgn)) {
  365.             could = true;
  366.             break;
  367.         };
  368.     };
  369.     DisposeRgn(rgn);
  370.     return could;
  371. }
  372.  
  373. /*______________________________________________________________________
  374.  
  375.     utl_DILoad - Load Disk Initialization Package.
  376.     
  377.     Exit:        Disk initialization package loaded.
  378.                 
  379.     This routine is identical to the DILoad routine (see IM II-396),
  380.     except that it closes any resource files opened by the routine.  This is
  381.     necessary to undo a bug in the DaynaFile software.  DaynaFile patches
  382.     DILoad.  The patch opens a resource file without closing it.  The
  383.     effect on Disinfectant if we don't do anything about this is that 
  384.     the DaynaFile icon is displayed in Disinfectant's main window instead of
  385.     Disinfectant's icon.  
  386. _____________________________________________________________________*/
  387.  
  388.  
  389. void utl_DILoad (void)
  390.  
  391. {
  392.     short            curResFile;            /* ref num of current resource file
  393.                                                 before calling DILoad */
  394.     short            topResFile;            /* ref num of top resource file
  395.                                                 after calling DILoad */
  396.     
  397.     curResFile = CurResFile();
  398.     DILoad();
  399.     while ((topResFile = CurResFile()) != curResFile) CloseResFile(topResFile);
  400. }
  401.  
  402. /*______________________________________________________________________
  403.  
  404.     utl_DoDiskInsert - Handle a disk inserted event.
  405.     
  406.     Entry:    message = message field from disk insertion event record
  407.                     = 16/MountVol return code, 16/drive number.
  408.     
  409.     Exit:        vRefNum = vol ref num of inserted volume.
  410.                 function result = error code.
  411.     
  412.     If MountVol returned an error code, the disk initialization package
  413.     is called to initialize the disk.
  414. _____________________________________________________________________*/
  415.  
  416.  
  417. OSErr utl_DoDiskInsert (long message, short *vRefNum)
  418.  
  419. {
  420.     OSErr                rCode;                /* result code */
  421.     short                driveNum;            /* drive number */
  422.     HParamBlockRec    pBlock;                /* vol info param block */
  423.     Handle            dlgHandle;            /* handle to disk init dialog */
  424.     Rect                dlgRect;                /* disk init dialog rectangle */
  425.     Point                where;                /* location of disk init dialog */
  426.     short                curVol;                /* index in VCB queue */
  427.     
  428.     /* Get result code and drive number from event message. */
  429.     
  430.     rCode = (message >> 16) & 0xffff;
  431.     driveNum = message & 0xffff;
  432.     
  433.     /* If the result code indicates an error, call DIBadMount to initialize
  434.         the disk. */
  435.         
  436.     if (rCode) {
  437.         
  438.         /* Center the disk initialization package dialog. */
  439.         
  440.         DILoad();
  441.         dlgHandle = GetResource('DLOG', -6047);
  442.         dlgRect = **(Rect**)dlgHandle;
  443.         utl_CenterDlogRect(&dlgRect, false);
  444.         SetPt(&where, dlgRect.left, dlgRect.top);
  445.         
  446.         /* Call DIBadMount. */
  447.         
  448.         if (rCode = DIBadMount(where, message)) return rCode;
  449.         
  450.     };
  451.     
  452.     /* Search mounted volumes to find inserted one. */
  453.     
  454.     pBlock.volumeParam.ioNamePtr = nil;
  455.     curVol = 0;
  456.     while (true) {
  457.         pBlock.volumeParam.ioVolIndex = ++curVol;
  458.         pBlock.volumeParam.ioVRefNum = 0;
  459.         if (rCode = PBHGetVInfo(&pBlock, false)) return rCode;
  460.         if (pBlock.volumeParam.ioVDrvInfo == driveNum) break;
  461.     };
  462.     *vRefNum = pBlock.volumeParam.ioVRefNum;
  463.     return noErr;
  464. }    
  465.  
  466. /*______________________________________________________________________
  467.  
  468.     utl_DrawGrowIcon - Draw Grow Icon.
  469.     
  470.     Entry:            theWindow = pointer to window.
  471.     
  472.     This routine is identical to the Window Manager routine 
  473.     DrawGrowIcon, except that it does not draw the lines enclosing the
  474.     scroll bars.
  475. _____________________________________________________________________*/
  476.  
  477.  
  478. void utl_DrawGrowIcon (WindowPtr theWindow) 
  479.  
  480. {
  481.     RgnHandle            clipRgn;            /* saved clip region */
  482.     Rect                    clipRect;        /* clip rectangle */
  483.     
  484.     clipRgn = NewRgn();
  485.     GetClip(clipRgn);
  486.     clipRect = theWindow->portRect;
  487.     clipRect.left = clipRect.right - 15;
  488.     clipRect.top = clipRect.bottom - 15;
  489.     ClipRect(&clipRect);
  490.     DrawGrowIcon(theWindow);
  491.     SetClip(clipRgn);
  492.     DisposeRgn(clipRgn);
  493. }
  494.  
  495. /*______________________________________________________________________
  496.  
  497.     utl_Ejectable - Test for ejectable volume.
  498.     
  499.     Entry:    vRefNum = volume reference number.
  500.     
  501.     Exit:        function result = true if volume is on an ejectable drive.
  502. _____________________________________________________________________*/
  503.  
  504.  
  505. Boolean utl_Ejectable (short vRefNum)
  506.     
  507. {
  508.     HParamBlockRec    pBlock;                /* vol info param block */
  509.     short                driveNum;            /* driver number of cur vol */
  510.     DrvQEl            *curDrive;            /* ptr to current drive queue element */
  511.     OSErr                rCode;                /* result code */
  512.     unsigned char    flagByte;            /* drive queue element flag byte */
  513.     
  514.     /* Get driveNum = drive number of drive containing volume. */
  515.     
  516.     pBlock.volumeParam.ioNamePtr = nil;
  517.     pBlock.volumeParam.ioVolIndex = 0;
  518.     pBlock.volumeParam.ioVRefNum = vRefNum;;
  519.     if (rCode = PBHGetVInfo(&pBlock, false)) return false;
  520.     driveNum = pBlock.volumeParam.ioVDrvInfo;
  521.     
  522.     /*    Walk the drive queue until we find driveNum.  The second byte in
  523.         the four flag bytes preceding the drive queue element is 8 or $48 
  524.         if the drive is nonejectable. */
  525.     
  526.     curDrive = (DrvQEl*)(GetDrvQHdr())->qHead;
  527.     while (true) {
  528.         if (curDrive->dQDrive == driveNum) {
  529.             flagByte = *((Ptr)curDrive - 3);
  530.             return (flagByte != 8 && flagByte != 0x48);
  531.         };
  532.         curDrive = (DrvQEl*)curDrive->qLink;
  533.         if (!curDrive) return false;
  534.     };
  535. }
  536.  
  537. /*______________________________________________________________________
  538.  
  539.     utl_FixStdFile - Fix Standard File Pacakge.
  540.     
  541.     This routine should be called before calling the Standard File 
  542.     package if there's any chance that SFSaveDisk might specify
  543.     a volume that has been umounted.  Standard File gets confused if this
  544.     happens and presents an alert telling the user that a "system error"
  545.     has occurred.
  546.     
  547.     This routine checks to make sure that SFSaveDisk specifies a volume 
  548.     that is still mounted.  If not, it sets it to the first mounted 
  549.     volume, and it sets CurDirStore to the root directory on that volume.
  550. _____________________________________________________________________*/
  551.                     
  552.                     
  553. void utl_FixStdFile (void)
  554.  
  555. {
  556.     ParamBlockRec    vBlock;            /* vol info param block */
  557.                                                 
  558.     vBlock.volumeParam.ioNamePtr = nil;
  559.     vBlock.volumeParam.ioVRefNum = -SFSaveDisk;
  560.     vBlock.volumeParam.ioVolIndex = 0;
  561.     if (PBGetVInfo(&vBlock, false)) {
  562.         vBlock.volumeParam.ioVolIndex = 1;
  563.         (void) PBGetVInfo(&vBlock, false);
  564.         SFSaveDisk = -vBlock.volumeParam.ioVRefNum;
  565.         if (FSFCBLen > 0) CurDirStore = fsRtDirID;
  566.     };
  567. }
  568.  
  569. /*______________________________________________________________________
  570.  
  571.     utl_FlashButton - Flash Dialog Button.
  572.     
  573.     Entry:    theDialog = pointer to dialog.
  574.                 itemNo = item number of button to flash.
  575.                 
  576.     The push button is inverted for 8 ticks.  See HIN #10.
  577. _____________________________________________________________________*/
  578.  
  579.  
  580. void utl_FlashButton (DialogPtr theDialog, short itemNo)
  581.  
  582. {
  583.     short            itemType;            /* item type */
  584.     Handle        item;                    /* item handle */
  585.     Rect            box;                    /* item rectangle */
  586.     short            roundFactor;        /* rounded corner factor for InvertRoundRect */
  587.     long            tickEnd;                /* tick count to end flash */
  588.     
  589.     GetDItem(theDialog, itemNo, &itemType, &item, &box);
  590.     SetPort(theDialog);
  591.     roundFactor = (box.bottom - box.top)>>1;
  592.     InvertRoundRect(&box, roundFactor, roundFactor);
  593.     tickEnd = TickCount() + 8;
  594.     while (TickCount() < tickEnd);
  595.     InvertRoundRect(&box, roundFactor, roundFactor);
  596. }
  597.  
  598. /*______________________________________________________________________
  599.  
  600.     utl_FrameItem
  601.     
  602.     Entry:    theWindow = pointer to dialog window.
  603.                 itemNo = dialog item number.
  604.     
  605.     Exit:        dialog item rectangle framed.
  606.                 
  607.     This function is for use as a Dialog Manager user item procedure.
  608.     It is particularly useful for drawing rules (straight lines).  Simply
  609.     define the user item rectangle with bottom=top+1 or right=left+1.
  610. _____________________________________________________________________*/
  611.  
  612.  
  613. pascal void utl_FrameItem (WindowPtr theWindow, short itemNo)
  614.  
  615. {
  616.     short            itemType;            /* item type */
  617.     Handle        item;                    /* item handle */
  618.     Rect            box;                    /* item rectangle */
  619.  
  620.     GetDItem(theWindow, itemNo, &itemType, &item, &box);
  621.     FrameRect(&box);
  622. }
  623.  
  624. /*______________________________________________________________________
  625.  
  626.     utl_GetApplVol - Get the volume reference number of the application volume.
  627.     
  628.     Exit:        function result = volume reference number of the volume 
  629.                     containing the current application.
  630. _____________________________________________________________________*/
  631.  
  632.  
  633. short utl_GetApplVol (void)
  634.  
  635. {
  636.     short            vRefNum;                /* vol ref num */
  637.  
  638.     GetVRefNum((CurMap), &vRefNum);
  639.     return vRefNum;
  640. }
  641.  
  642. /*______________________________________________________________________
  643.  
  644.     utl_GetBlessedWDRefNum - Get the working directory reference number of
  645.         the Blessed Folder.
  646.     
  647.     
  648.     Exit:        function result = wdRefNum of blessed folder
  649. _____________________________________________________________________*/
  650.  
  651.  
  652. short utl_GetBlessedWDRefNum (void)
  653.  
  654. {
  655.     if (!GotSysEnviron) GetSysEnvirons();
  656.     return TheWorld.sysVRefNum;
  657. }
  658.  
  659. /*______________________________________________________________________
  660.  
  661.     utl_GetFontNumber - Get Font Number.
  662.     
  663.     Entry:    fontName = font name.
  664.     
  665.     Exit:        function result = true if font exists.
  666.                 fontNum = font number.
  667.                 
  668.     Copied from TN 191.
  669. _____________________________________________________________________*/
  670.  
  671.  
  672. Boolean utl_GetFontNumber (Str255 *fontName, short *fontNum)
  673.  
  674. {
  675.     Str255        systemFontName;
  676.     
  677.     GetFNum(fontName, fontNum);
  678.     if (*fontNum) {
  679.         return true;
  680.     } else {
  681.         GetFontName(0, systemFontName);
  682.         return EqualString((StringPtr)fontName, (StringPtr)&systemFontName, false, false);
  683.     };
  684. }
  685.  
  686. /*______________________________________________________________________
  687.  
  688.     utl_GetLongSleep - Get Long Sleep Time.
  689.     
  690.     Exit:            function result = long sleep time.
  691.     
  692.     Returns the largest positive long integer (0x7fffffff) if the 
  693.     system version is > 0x04ff, else returns 50.  See TN 177.
  694. _____________________________________________________________________*/
  695.  
  696.  
  697. long utl_GetLongSleep (void)
  698.  
  699. {
  700.     if (!GotSysEnviron) GetSysEnvirons();
  701.     return (TheWorld.systemVersion > 0x04ff) ? 0x7fffffff : 50;
  702. }
  703.  
  704. /*______________________________________________________________________
  705.  
  706.     utl_GetMBarHeight - Get Menu Bar Height
  707.     
  708.     Exit:        function result = menu bar height.
  709.     
  710.     See TN 117.
  711. _____________________________________________________________________*/
  712.  
  713.  
  714. short utl_GetMBarHeight (void)
  715.  
  716. {
  717.     static short        mBHeight = 0;
  718.     
  719.     if (!mBHeight) {
  720.         mBHeight = utl_Rom64() ? 20 : MBarHeight;
  721.     };
  722.     return mBHeight;
  723. }
  724.  
  725. /*______________________________________________________________________
  726.  
  727.     utl_GetNewControl - Get New Control.
  728.     
  729.     Entry:    controlID = resource id of CNTL resource.
  730.                 theWindow = pointer to window record.
  731.                 
  732.     Exit:        function result = handle to new control record.
  733.                 
  734.     This routine is identical to the Control Manager routine GetNewControl,
  735.     except it does not release or make purgeable the CNTL resource.
  736. _____________________________________________________________________*/
  737.  
  738.  
  739. ControlHandle utl_GetNewControl (short controlID, 
  740.     WindowPtr theWindow)
  741.     
  742. {
  743.     Rect            boundsRect;            /* boundary rectangle */
  744.     short            value;                /* initial control value */
  745.     Boolean        visible;                /* true if visible */
  746.     short            max;                    /* max control value */
  747.     short            min;                    /* min control value */
  748.     short            procID;                /* window proc id */
  749.     long            refCon;                /* refCon field for window record */
  750.     Str255        title;                /* window title */
  751.     Handle        theRez;                /* handle to CNTL resource */
  752.     
  753.     theRez = GetResource('CNTL', controlID);
  754.     boundsRect = *(Rect*)(*theRez);
  755.     value = *(short*)(*theRez+8);
  756.     visible = *(Boolean*)(*theRez+10);
  757.     max = *(short*)(*theRez+12);
  758.     min = *(short*)(*theRez+14);
  759.     procID = *(short*)(*theRez+16);
  760.     refCon = *(long*)(*theRez+18);
  761.     utl_CopyPString(&title, (Str255 *)(*theRez+22));
  762.     return NewControl(theWindow, &boundsRect, title, visible, value,
  763.         min, max, procID, refCon);
  764. }
  765.  
  766. /*______________________________________________________________________
  767.  
  768.     utl_GetNewDialog - Get New Dialog.
  769.     
  770.     Entry:    dialogID = resource id of DLOG resource.
  771.                 dStorage = pointer to dialog record.
  772.                 behind = window to insert in back of.
  773.                 
  774.     Exit:        function result = pointer to new dialog record.
  775.                 
  776.     This routine is identical to the Dialog Manager routine GetNewDialog,
  777.     except it does not release or make purgeable the DLOG resource.
  778. _____________________________________________________________________*/
  779.  
  780.  
  781. DialogPtr utl_GetNewDialog (short dialogID, Ptr dStorage, 
  782.     WindowPtr behind)
  783.     
  784. {
  785.     Rect            boundsRect;            /* boundary rectangle */
  786.     short            procID;                /* window proc id */
  787.     Boolean        visible;                /* true if visible */
  788.     Boolean        goAwayFlag;            /* true if window has go away box */
  789.     long            refCon;                /* refCon field for window record */
  790.     Str255        title;                /* window title */
  791.     Handle        theRez;                /* handle to DLOG resource */
  792.     short            itemID;                /* rsrc id of item list */
  793.     Handle        items;                /* handle to item list */
  794.     
  795.     theRez = GetResource('DLOG', dialogID);
  796.     boundsRect = *(Rect*)(*theRez);
  797.     procID = *(short*)(*theRez+8);
  798.     visible = *(Boolean*)(*theRez+10);
  799.     goAwayFlag = *(Boolean*)(*theRez+12);
  800.     refCon = *(long*)(*theRez+14);
  801.     itemID = *(short*)(*theRez+18);
  802.     utl_CopyPString(&title, (Str255 *)(*theRez+20));
  803.     items = GetResource('DITL', itemID);
  804.     return NewDialog((DialogPeek)dStorage, &boundsRect, title, visible, procID, behind,
  805.         goAwayFlag, refCon, items);
  806. }
  807.  
  808. /*______________________________________________________________________
  809.  
  810.     utl_GetNewWindow - Get New Window.
  811.     
  812.     Entry:    windowID = resource id of WIND resource.
  813.                 wStorage = pointer to window record.
  814.                 behind = window to insert in back of.
  815.                 
  816.     Exit:        function result = pointer to new window record.
  817.                 
  818.     This routine is identical to the Window Manager routine GetNewWindow,
  819.     except it does not release or make purgeable the WIND resource.
  820. _____________________________________________________________________*/
  821.  
  822.  
  823. WindowPtr utl_GetNewWindow (short windowID, Ptr wStorage, 
  824.     WindowPtr behind)
  825.     
  826. {
  827.     Rect            boundsRect;            /* boundary rectangle */
  828.     short            procID;                /* window proc id */
  829.     Boolean        visible;                /* true if visible */
  830.     Boolean        goAwayFlag;            /* true if window has go away box */
  831.     long            refCon;                /* refCon field for window record */
  832.     Str255        title;                /* window title */
  833.     Handle        theRez;                /* handle to WIND resource */
  834.     
  835.     theRez = GetResource('WIND', windowID);
  836.     boundsRect = *(Rect*)(*theRez);
  837.     procID = *(short*)(*theRez+8);
  838.     visible = *(Boolean*)(*theRez+10);
  839.     goAwayFlag = *(Boolean*)(*theRez+12);
  840.     refCon = *(long*)(*theRez+14);
  841.     utl_CopyPString(&title, (Str255 *)(*theRez+18));
  842.     return NewWindow((WindowPeek)wStorage, &boundsRect, title, visible, procID, behind,
  843.         goAwayFlag, refCon);
  844. }
  845.  
  846. /*______________________________________________________________________
  847.  
  848.     utl_GetSysVol - Get the volume reference number of the system volume.
  849.     
  850.     Exit:        function result = volume reference number of the volume 
  851.                     containing the currently active system file.
  852. _____________________________________________________________________*/
  853.  
  854.  
  855. short utl_GetSysVol (void)
  856.  
  857. {
  858.     
  859.     short            vRefNum;                /* vol ref num */
  860.  
  861.     GetVRefNum(SysMap, &vRefNum);
  862.     return vRefNum;
  863. }
  864.  
  865. /*______________________________________________________________________
  866.  
  867.     utl_GetSysWD - Get WDRefNum of Blessed Folder.
  868.     
  869.     Exit:        function result = WDRefNum of Blessed Folder.
  870. _____________________________________________________________________*/
  871.  
  872.  
  873. short utl_GetSysWD (void)
  874.  
  875. {
  876.     if (!GotSysEnviron) GetSysEnvirons();
  877.     return TheWorld.sysVRefNum;
  878. }
  879.  
  880. /*______________________________________________________________________
  881.  
  882.     utl_GetWindGD - Get the GDevice containing a window.
  883.     
  884.     Entry:    theWindow = pointer to window.
  885.     
  886.     Exit:        gd = handle to GDevice, or nil if no color QD.
  887.                 screenRect = bounding rectangle of GDevice, or 
  888.                     qd.screenBits.bounds if no color QD.
  889.                 windRect = content rectangle of window, in global
  890.                     coords.
  891.                 hasMB = true if this screen contains the menu bar.
  892.                 
  893.     The routine determines the GDevice (screen) containing the maximum
  894.     intersection with a window.  See TN 79.
  895. _____________________________________________________________________*/
  896.  
  897.  
  898. void utl_GetWindGD (WindowPtr theWindow, GDHandle *gd, 
  899.     Rect *screenRect, Rect *windRect, Boolean *hasMB)
  900.     
  901. {
  902.     GrafPtr            savePort;            /* saved grafport */
  903.     Rect                sectRect;            /* intersection rect */
  904.     GDHandle            curDevice;            /* current GDevice */
  905.     GDHandle            dominantDevice;    /* dominant GDevice */
  906.     long                sectArea;            /* intersection area */
  907.     long                maxArea;                /* max intersection area */
  908.     
  909.     *windRect = theWindow->portRect;
  910.     GetPort(&savePort);
  911.     SetPort(theWindow);
  912.     LocalToGlobal((Point*)&windRect->top);
  913.     LocalToGlobal((Point*)&windRect->bottom);
  914.     if (utl_HaveColor()) {
  915.         windRect->top -= titleBarHeight;
  916.         curDevice = GetDeviceList();
  917.         maxArea = 0;
  918.         dominantDevice = nil;
  919.         while (curDevice) {
  920.             if (TestDeviceAttribute(curDevice, screenDevice) &&
  921.                 TestDeviceAttribute(curDevice, screenActive)) {
  922.                 SectRect(windRect, &(**curDevice).gdRect, §Rect);
  923.                 sectArea = (long)(sectRect.right - sectRect.left) * 
  924.                     (long)(sectRect.bottom - sectRect.top);
  925.                 if (sectArea > maxArea) {
  926.                     maxArea = sectArea;
  927.                     dominantDevice = curDevice;
  928.                 };
  929.             };
  930.             curDevice = GetNextDevice(curDevice);
  931.         };
  932.         windRect->top += titleBarHeight;
  933.         if (dominantDevice) {
  934.             *gd = dominantDevice;
  935.             *screenRect = (**dominantDevice).gdRect;
  936.             *hasMB = dominantDevice == GetMainDevice();
  937.         } else {
  938.             *gd = nil;
  939.             *screenRect = screenBits.bounds;
  940.             *hasMB = true;
  941.         };    
  942.     } else {
  943.         *gd = nil;
  944.         *screenRect = screenBits.bounds;
  945.         *hasMB = true;
  946.     };
  947.     SetPort(savePort);
  948. }
  949.  
  950. /*______________________________________________________________________
  951.  
  952.     utl_GetVolFilCnt - Get the number of files on a volume.
  953.     
  954.     Entry:    volRefNum = volume reference number of volume.
  955.     
  956.     Exit:        function result = number of files on volume.
  957.     
  958.     For serdver volumes this function always returns 0.
  959. _____________________________________________________________________*/
  960.  
  961.  
  962. long utl_GetVolFilCnt (short volRefNum)
  963.  
  964. {
  965.     HParamBlockRec        pBlock;        /* param block for PHBGetVInfo */
  966.     
  967.     pBlock.volumeParam.ioNamePtr = nil;
  968.     pBlock.volumeParam.ioVRefNum = volRefNum;
  969.     pBlock.volumeParam.ioVolIndex = 0;
  970.     (void) PBHGetVInfo(&pBlock, false);
  971.     return pBlock.volumeParam.ioVFilCnt;
  972. }
  973.  
  974. /*______________________________________________________________________
  975.  
  976.     utl_HaveColor - Determine if system has color QuickDraw.
  977.  
  978.     Exit:        function result = true if we have color QD.
  979. _____________________________________________________________________*/
  980.  
  981.  
  982. Boolean utl_HaveColor (void)
  983.  
  984. {
  985.     if (!GotSysEnviron) GetSysEnvirons();
  986.     return TheWorld.hasColorQD;
  987. }
  988.  
  989. /*______________________________________________________________________
  990.  
  991.     utl_HaveSound - Determine if system has the Sound Manager.
  992.  
  993.     Exit:        function result = true if we have sound.
  994. _____________________________________________________________________*/
  995.  
  996.  
  997. Boolean utl_HaveSound (void)
  998.  
  999. {
  1000.     if (!GotSysEnviron) GetSysEnvirons();
  1001.     return (TheWorld.systemVersion >= 0x0602);
  1002. }
  1003.  
  1004. /*______________________________________________________________________
  1005.  
  1006.     utl_InitSpinCursor - Initialize animated cursor.
  1007.     
  1008.     Entry:    cursArray = array of handles to cursors.
  1009.                 numCurs = number of cursors to rotate.
  1010.                 tickInterval = interval between cursor rotations.
  1011. _____________________________________________________________________*/
  1012.  
  1013.  
  1014. void utl_InitSpinCursor (CursHandle *cursArray, short numCurs, 
  1015.     short tickInterval)
  1016.  
  1017. {
  1018.     CursHandle        h;
  1019.  
  1020.     CursArray = cursArray;
  1021.     CurCurs = 0;
  1022.     NumCurs = numCurs;
  1023.     TickInterval = tickInterval;
  1024.     LastTick = TickCount();
  1025.     h = *cursArray;
  1026.     SetCursor(*h);
  1027. }
  1028.  
  1029. /*______________________________________________________________________
  1030.  
  1031.     utl_InvalGrow - Invalidate Grow Icon.
  1032.     
  1033.     Entry:            theWindow = pointer to window.
  1034.     
  1035.     This routine should be called before and after calling SizeWindow
  1036.     for windows with grow icons.
  1037. _____________________________________________________________________*/
  1038.  
  1039.  
  1040. void utl_InvalGrow (WindowPtr theWindow)
  1041.  
  1042. {
  1043.     Rect            r;            /* rect to be invalidated */
  1044.     
  1045.     r = theWindow->portRect;
  1046.     r.top = r.bottom - 15;
  1047.     r.left = r.right - 15;
  1048.     InvalRect(&r);
  1049. };
  1050.  
  1051. /*______________________________________________________________________
  1052.  
  1053.     utl_IsDAWindow - Check to see if a window is a DA.
  1054.     
  1055.     Entry:    theWindow = pointer to dialog window.
  1056.                 
  1057.     
  1058.     Exit:        function result = true if DA window.
  1059. _____________________________________________________________________*/
  1060.  
  1061.  
  1062. Boolean utl_IsDAWindow (WindowPtr theWindow)
  1063.  
  1064. {
  1065.     return ((WindowPeek)theWindow)->windowKind < 0;
  1066. }
  1067.  
  1068. /*______________________________________________________________________
  1069.  
  1070.     utl_IsLaser - Check Printer for LaserWriter.
  1071.     
  1072.     Entry:    hPrint = handle to print record.
  1073.     
  1074.     Exit:        function result = true if LaserWriter.
  1075.     
  1076. _____________________________________________________________________*/
  1077.  
  1078.  
  1079. Boolean utl_IsLaser (THPrint hPrint)
  1080.  
  1081. {
  1082.     unsigned char    wDev;                /* printer device */
  1083.  
  1084.     wDev = (**hPrint).prStl.wDev >> 8;
  1085.     return wDev==3 || wDev==4;
  1086. }
  1087.  
  1088. /*______________________________________________________________________
  1089.  
  1090.     utl_LockControls - Lock Window Controls
  1091.     
  1092.     Entry:    theWindow = pointer to window.
  1093.     
  1094.     This routine moves all the control records in a window high and
  1095.     locks them.  It should be called immediately after creating a new
  1096.     window, and before drawing it.  It works around errors in the Control
  1097.     Manager which showed up when I was testing with TMON's heap scramble and
  1098.     purge option.
  1099. _____________________________________________________________________*/
  1100.  
  1101.  
  1102. void utl_LockControls (WindowPtr theWindow)
  1103.  
  1104. {
  1105.     ControlHandle        theControl;        /* handle to control */
  1106.  
  1107.     theControl = ((WindowPeek)theWindow)->controlList;
  1108.     while (theControl) {
  1109.         MoveHHi((Handle)theControl);
  1110.         HLock((Handle)theControl);
  1111.         theControl = (**theControl).nextControl;
  1112.     };
  1113. }
  1114.  
  1115. /*______________________________________________________________________
  1116.  
  1117.     utl_PlotSmallIcon - Draw a small icon.
  1118.     
  1119.     Entry:    theRect = rectangle in which to draw the small icon.
  1120.                 theHandle = handle to small icon.
  1121.                     
  1122.     For best results, the rectangle should be exactly 16 pixels square.
  1123. _____________________________________________________________________*/
  1124.  
  1125.  
  1126. void utl_PlotSmallIcon (Rect *theRect, Handle theHandle)
  1127.  
  1128. {
  1129.     BitMap        srcBits;            /* Source bitmap for CopyBits */
  1130.     
  1131.     MoveHHi(theHandle);
  1132.     HLock(theHandle);
  1133.     srcBits.baseAddr = *theHandle;
  1134.     srcBits.rowBytes = 2;
  1135.     SetRect(&srcBits.bounds, 0, 0, 16, 16);
  1136.     CopyBits(&srcBits, &thePort->portBits, &srcBits.bounds, theRect,
  1137.         srcCopy, nil);
  1138.     HUnlock(theHandle);
  1139. }
  1140.  
  1141. /*______________________________________________________________________
  1142.  
  1143.     utl_PlugParams - Plug parameters into message.
  1144.     
  1145.     Entry:    line1 = input line.
  1146.                 p0, p1, p2, p3 = parameters.
  1147.                     
  1148.     Exit:        line2 = output line.
  1149.                     
  1150.     This routine works just like the toolbox routine ParamText.
  1151.     The input line may contain place-holders ^0, ^1, ^2, and ^3, 
  1152.     which are replaced by the parameters p0-p3.  The input line
  1153.     must not contain any other ^ characters.  The input and output lines
  1154.     may not be the same string.  Pass nil for parameters which don't
  1155.     occur in line1.  If the output line exceeds 255 characters it's
  1156.     truncated.
  1157. _____________________________________________________________________*/
  1158.  
  1159.  
  1160. void utl_PlugParams (Str255 *line1, Str255 *line2, Str255 *p0, 
  1161.     Str255 *p1, Str255 *p2, Str255 *p3)
  1162.  
  1163. {
  1164.     unsigned char    *in;            /* pointer to cur pos in input line */
  1165.     unsigned char    *out;            /* pointer to cur pos in output line */
  1166.     unsigned char    *inEnd;        /* pointer to end of input line */
  1167.     unsigned char    *outEnd;        /* pointer to end of output line */
  1168.     unsigned char    *param;        /* pointer to param to be plugged */
  1169.     short                len;            /* length of param */
  1170.     
  1171.     in = (unsigned char *)line1 + 1;
  1172.     out = (unsigned char *)line2 + 1;
  1173.     inEnd = in + (*line1)[0];
  1174.     outEnd = out + 256;
  1175.     while (in < inEnd ) {
  1176.         if (*in == '^') {
  1177.             in++;
  1178.             if (in >= inEnd) break;
  1179.             switch (*in++) {
  1180.                 case '0':
  1181.                     param = (unsigned char *)p0;
  1182.                     break;
  1183.                 case '1':
  1184.                     param = (unsigned char *)p1;
  1185.                     break;
  1186.                 case '2':
  1187.                     param = (unsigned char *)p2;
  1188.                     break;
  1189.                 case '3':
  1190.                     param = (unsigned char *)p3;
  1191.                     break;
  1192.                 default:
  1193.                     param = nil;
  1194.                     continue;
  1195.             };
  1196.             if (!param) continue;
  1197.             len = *param;
  1198.             if (out + len > outEnd) len = outEnd - out;
  1199.             memcpy(out, param+1, len);
  1200.             out += len;
  1201.         } else {
  1202.             if (out >= outEnd) break;
  1203.             *out++ = *in++;
  1204.         };
  1205.     };
  1206.     (*line2)[0] = out - ((unsigned char *)line2 + 1);
  1207. };
  1208.  
  1209. /*______________________________________________________________________
  1210.  
  1211.     utl_RestoreWindowPos - Restore Window Position.
  1212.     
  1213.     Entry:    theWindow = pointer to window.
  1214.                 userState = saved user state rectangle for the window.
  1215.                 zoomed = true if window in zoomed state when saved.
  1216.                 offset = pixel offset used in DragRect calls.
  1217.                 computeStdState = pointer to function to compute standard state.
  1218.                 computeDefState = pointer to function to compute default state.
  1219.     
  1220.     Exit:        window position, size, and zoom state restored.
  1221.                 userState = new user state.
  1222.                 
  1223.     See HIN 6: "When reopening a movable window, check its saved 
  1224.     position.  If the window is in a position to which the user could
  1225.     have dragged it, then leave it there.  If the window can be zoomed
  1226.     and was in the zoomed state when it was last closed, put it in the
  1227.     zoomed state again.  (Note that the current and previous zoomed states
  1228.     are not necessarily the same, since the window may be reopened on a
  1229.     different monitor.)  If the window is not in a position to which the
  1230.     user could have dragged it, then it must be relocated, so use the
  1231.     default location.  However, do not automatically use the default size
  1232.     when using the default location; if the entire window would be visible
  1233.     using the default location and stored size, then use the stored size."
  1234.     
  1235.     The "offset" parameter is usually 4.  When initializing the boundary rectangle
  1236.     for DragWindow calls, normally the boundary rectangle of the desktop gray
  1237.     region is inset by 4 pixels.  If some value other than 4 is used, it should
  1238.     be passed to RestoreWindowPos as the "offset" parameter.
  1239.     
  1240.     The computeStdState function is passed a pointer to the window.  Given
  1241.     the userState in the window zoom info, it must compute the standard
  1242.     (zoom) state in the window zoom info.  This is an application-dependent
  1243.     function.
  1244.     
  1245.     The computeDefState function must determine the default position and
  1246.     size of a new window, and return the result as a rectangle.  This may
  1247.     involve invocation of a staggering algorithm or some other algorithm.
  1248.     This is an application-dependent function.
  1249. _____________________________________________________________________*/
  1250.  
  1251.  
  1252. void utl_RestoreWindowPos (WindowPtr theWindow, Rect *userState, 
  1253.     Boolean zoomed, short offset,
  1254.     utl_ComputeStdStatePtr computeStdState,
  1255.     utl_ComputeDefStatePtr computeDefState)
  1256.  
  1257. {
  1258.     WindowPeek        w;                    /* window pointer */
  1259.     short                userHeight;        /* height of userState */
  1260.     short                userWidth;        /* width of userState */
  1261.     Rect                r;                    /* scratch rectangle */
  1262.     RgnHandle        rgn;                /* scratch region */
  1263.     Rect                stdState;        /* standard state */
  1264.     short                windHeight;        /* window height */
  1265.     short                windWidth;        /* window width */
  1266.  
  1267.     w = (WindowPeek)theWindow;
  1268.     if (!utl_CouldDrag(userState, offset)) {
  1269.         userHeight = userState->bottom - userState->top;
  1270.         userWidth = userState->right - userState->left;
  1271.         (*computeDefState)(theWindow, userState);
  1272.         if (!zoomed) {
  1273.             r = *userState;
  1274.             r.bottom = r.top + userHeight;
  1275.             r.right = r.left + userWidth;
  1276.             r.top -= titleBarHeight;
  1277.             InsetRect(&r, -1, -1);
  1278.             rgn = NewRgn();
  1279.             RectRgn(rgn, &r);
  1280.             DiffRgn(rgn, GrayRgn, rgn);
  1281.             if (EmptyRgn(rgn)) {
  1282.                 userState->bottom = userState->top + userHeight;
  1283.                 userState->right = userState->left + userWidth;
  1284.             };
  1285.             DisposeRgn(rgn);
  1286.         };
  1287.     };
  1288.     MoveWindow(theWindow, userState->left, userState->top, false);
  1289.     windHeight = userState->bottom - userState->top;
  1290.     windWidth = userState->right - userState->left;
  1291.     SizeWindow(theWindow, windWidth, windHeight, true);
  1292.     if (w->dataHandle && w->spareFlag) {
  1293.         (**((WStateData**)w->dataHandle)).userState = *userState;
  1294.         (*computeStdState)(theWindow);
  1295.         if (zoomed) {
  1296.             stdState = (**((WStateData**)w->dataHandle)).stdState;
  1297.             MoveWindow(theWindow, stdState.left, stdState.top, false);
  1298.             windHeight = stdState.bottom - stdState.top;
  1299.             windWidth = stdState.right - stdState.left;
  1300.             SizeWindow(theWindow, windWidth, windHeight, true);
  1301.         };
  1302.     };
  1303. }
  1304.  
  1305. /*______________________________________________________________________
  1306.  
  1307.     utl_Rom64 - Check to see if we have the old 64K ROM.
  1308.     
  1309.     Exit:        function result = true if 64K ROM.
  1310. _____________________________________________________________________*/
  1311.  
  1312.  
  1313. Boolean utl_Rom64 (void)
  1314.  
  1315. {
  1316.     return ROM85 < 0;
  1317. }
  1318.  
  1319. /*______________________________________________________________________
  1320.  
  1321.     utl_RFSanity - Check a Resource File's Sanity.
  1322.     
  1323.     Entry:    *fName = file name.
  1324.     
  1325.     Exit:        *sane = true if resource fork is sane.
  1326.                 function result = error code.
  1327.                 
  1328.     This routine checks the sanity of a resource file.  The Resource Manager
  1329.     does not do error checking, and can bomb or hang if you use it to open
  1330.     a damaged resource file.  This routine can be called first to precheck
  1331.     the file.
  1332.     
  1333.     The routine checks the entire resource map.
  1334.     
  1335.     It is the caller's responsibility to set the proper default volume and
  1336.     directory.
  1337. _____________________________________________________________________*/
  1338.  
  1339.  
  1340. OSErr utl_RFSanity (Str255 *fName, Boolean *sane)
  1341.  
  1342. {
  1343.     ParamBlockRec        fBlock;            /* file param block */
  1344.     short                    refNum;            /* file refnum */
  1345.     long                    count;            /* number of bytes to read */
  1346.     long                    logEOF;            /* logical EOF */
  1347.     Ptr                    map;                /* pointer to resource map */
  1348.     unsigned long        dataLWA;            /* offset in file of data end */
  1349.     unsigned long        mapLWA;            /* offset in file of map end */
  1350.     unsigned short        typeFWA;            /* offset from map begin to type list */
  1351.     unsigned short        nameFWA;            /* offset from map begin to name list */
  1352.     unsigned char        *pType;            /* pointer into type list */
  1353.     unsigned char        *pName;            /* pointer to start of name list */
  1354.     unsigned char        *pMapEnd;        /* pointer to end of map */
  1355.     short                    nType;            /* number of resource types in map */
  1356.     unsigned char        *pTypeEnd;        /* pointer to end of type list */
  1357.     short                    nRes;                /* number of resources of given type */
  1358.     unsigned short        refFWA;            /* offset from type list to ref list */
  1359.     unsigned char        *pRef;            /* pointer into reference list */
  1360.     unsigned char        *pRefEnd;        /* pointer to end of reference list */
  1361.     unsigned short        resNameFWA;        /* offset from name list to resource name */
  1362.     unsigned char        *pResName;        /* pointer to resource name */
  1363.     unsigned long        resDataFWA;        /* offset from data begin to resource data */
  1364.     Boolean                mapOK;            /* true if map is sane */
  1365.     OSErr                    rCode;            /* error code */
  1366.     
  1367.     struct {
  1368.         unsigned long        dataFWA;        /* offset in file of data */
  1369.         unsigned long        mapFWA;        /* offset in file of map */
  1370.         unsigned long        dataLen;        /* data area length */
  1371.         unsigned long        mapLen;        /* map area length */
  1372.     } header;
  1373.     
  1374.     /* Open the resource file. */
  1375.     
  1376.     fBlock.ioParam.ioNamePtr = (StringPtr)fName;
  1377.     fBlock.ioParam.ioVRefNum = 0;
  1378.     fBlock.ioParam.ioVersNum = 0;
  1379.     fBlock.ioParam.ioPermssn = fsRdPerm;
  1380.     fBlock.ioParam.ioMisc = nil;
  1381.     if (rCode = PBOpenRF(&fBlock, false)) {
  1382.         if (rCode == fnfErr) {
  1383.             *sane = true;
  1384.             return noErr;
  1385.         } else {
  1386.             return rCode;
  1387.         };
  1388.     };
  1389.     
  1390.     /* Get the logical eof of the file. */
  1391.     
  1392.     refNum = fBlock.ioParam.ioRefNum;
  1393.     if (rCode = GetEOF(refNum, &logEOF)) return rCode;
  1394.     if (!logEOF) {
  1395.         *sane = true;
  1396.         if (rCode = FSClose(refNum)) return rCode;
  1397.         return noErr;
  1398.     };
  1399.     
  1400.     /* Read and validate the resource header. */
  1401.     
  1402.     count = 16;
  1403.     if (rCode = FSRead(refNum, &count, (Ptr)&header)) {
  1404.         FSClose(refNum);
  1405.         return rCode;
  1406.     };
  1407.     dataLWA = header.dataFWA + header.dataLen;
  1408.     mapLWA = header.mapFWA + header.mapLen;
  1409.     mapOK = count == 16 && header.mapLen > 28 &&
  1410.         header.dataFWA < 0x01000000 && header.mapFWA < 0x01000000 &&
  1411.         dataLWA <= logEOF && mapLWA <= logEOF &&
  1412.         (dataLWA <= header.mapFWA || mapLWA <= header.dataFWA);
  1413.         
  1414.     /* Read the resource map. */
  1415.     
  1416.     map = nil;
  1417.     if (mapOK) {
  1418.         map = NewPtr(header.mapLen);
  1419.         if (!(rCode = SetFPos(refNum, fsFromStart, header.mapFWA))) {
  1420.             count = header.mapLen;
  1421.             rCode = FSRead(refNum, &count, map);
  1422.         };
  1423.     };
  1424.     
  1425.     /* Verify the type list and name list offsets. */
  1426.     
  1427.     if (!rCode) {
  1428.         typeFWA = *(unsigned short*)(map+24);
  1429.         nameFWA = *(unsigned short*)(map+26);
  1430.         mapOK = typeFWA == 28 && nameFWA >= typeFWA && nameFWA <= header.mapLen &&
  1431.             !(typeFWA & 1) && !(nameFWA & 1);
  1432.     };
  1433.     
  1434.     /* Verify the type list, reference lists, and name list. */
  1435.     
  1436.     if (mapOK) {
  1437.         pType = (unsigned char *)map + typeFWA;
  1438.         pName = (unsigned char *)map + nameFWA;
  1439.         pMapEnd = (unsigned char *)map + header.mapLen;
  1440.         nType = *(short*)pType + 1;
  1441.         pType += 2;
  1442.         pTypeEnd = pType + (nType<<3);
  1443.         if (mapOK = pTypeEnd <= pMapEnd) {
  1444.             while (pType < pTypeEnd) {
  1445.                 nRes = *(short*)(pType+4) + 1;
  1446.                 refFWA = *(unsigned short*)(pType+6);
  1447.                 pRef = (unsigned char *)map + typeFWA + refFWA;
  1448.                 pRefEnd = pRef + 12*nRes;
  1449.                 if (!(mapOK = pRef >= pTypeEnd && pRef < pName && 
  1450.                     !(refFWA & 1))) break;
  1451.                 while (pRef < pRefEnd) {
  1452.                     resNameFWA = *(unsigned short*)(pRef+2);
  1453.                     if (resNameFWA != 0xFFFF) {
  1454.                         pResName = pName + resNameFWA;
  1455.                         if (!(mapOK = pResName + *pResName < pMapEnd)) break;
  1456.                     };
  1457.                     resDataFWA = *(unsigned long*)(pRef+4) & 0x00FFFFFF;
  1458.                     if (!(mapOK = header.dataFWA + resDataFWA < dataLWA)) break;
  1459.                     pRef += 12;
  1460.                 };
  1461.                 if (!mapOK) break;
  1462.                 pType += 8;
  1463.             };
  1464.         };
  1465.     };
  1466.     
  1467.     /* Dispose of the resource map, close the file and return. */
  1468.     
  1469.     if (map) DisposPtr(map);
  1470.     if (!rCode) {
  1471.         rCode = FSClose(refNum);
  1472.     } else {
  1473.         (void) FSClose(refNum);
  1474.     };
  1475.     *sane = mapOK;
  1476.     return rCode;
  1477. }
  1478.  
  1479. /*______________________________________________________________________
  1480.  
  1481.     utl_SaveWindowPos - Save Window Position.
  1482.     
  1483.     Entry:    theWindow = window pointer.
  1484.     
  1485.     Exit:        userState = user state rectangle of the window.
  1486.                 zoomed = true if window zoomed.
  1487.                 
  1488.     See HIN 6: "Before closing a movable window, check to see if its
  1489.     location or size have changed.  If so, save the new location and
  1490.     size.  If the window can be zoomed, save the user state and also 
  1491.     save whether or not the window is in the zoomed (standard) sate."
  1492.     
  1493.     We assume in this routine that the caller has already kept track
  1494.     of the fact that this window's location or size has changed, and that
  1495.     we do indeed need to save the location and size.
  1496.     
  1497.     This routine only works if the window's origin has not been offset.
  1498. _____________________________________________________________________*/
  1499.  
  1500.  
  1501. void utl_SaveWindowPos (WindowPtr theWindow, Rect *userState, Boolean *zoomed)
  1502.  
  1503. {
  1504.     GrafPtr        savedPort;        /* saved grafport */
  1505.     WindowPeek    w;                    /* window pointer */
  1506.     Rect            curState;        /* current window rect, global coords */
  1507.     Rect            stdState;        /* standard (zoom) rect, global coords */
  1508.     Point            p;                    /* scratch point */
  1509.     Rect            r;                    /* scratch rect */
  1510.     
  1511.     GetPort(&savedPort);
  1512.     SetPort(theWindow);
  1513.     SetPt(&p, 0, 0);
  1514.     LocalToGlobal(&p);
  1515.     curState = theWindow->portRect;
  1516.     OffsetRect(&curState, p.h, p.v);
  1517.     w = (WindowPeek)theWindow;
  1518.     if (w->dataHandle && w->spareFlag) {
  1519.         /* This window supports zooming */
  1520.         /* Determine if window is zoomed.  The criteria is that both the
  1521.             top left and bottom right corners of the current window rectangle
  1522.             and the standard (zoom) rectangle must be within 7 pixels of each
  1523.             other.  This is the same algorithm as the one used by the standard
  1524.             system window definition function. */
  1525.         *zoomed = false;
  1526.         stdState = (**((WStateData**)w->dataHandle)).stdState;
  1527.         SetPt(&p, curState.left, curState.top);
  1528.         SetRect(&r, stdState.left, stdState.top, stdState.left, stdState.top);
  1529.         InsetRect(&r, -7, -7);
  1530.         if (PtInRect(p, &r)) {
  1531.             SetPt(&p, curState.right, curState.bottom);
  1532.             SetRect(&r, stdState.right, stdState.bottom, stdState.right,
  1533.                 stdState.bottom);
  1534.             InsetRect(&r, -7, -7);
  1535.             *zoomed = PtInRect(p, &r);
  1536.         };
  1537.         if (*zoomed) {
  1538.             *userState = (**((WStateData**)w->dataHandle)).userState;
  1539.         } else {
  1540.             *userState = curState;
  1541.         };
  1542.     } else {
  1543.         /* This window does not support zooming. */
  1544.         *zoomed = false;
  1545.         *userState = curState;
  1546.     };
  1547.     SetPort(savedPort);
  1548. }
  1549.  
  1550. /*______________________________________________________________________
  1551.  
  1552.     utl_ScaleFontSize - Scale Font Size
  1553.     
  1554.     Entry:    fontNum = font number.
  1555.                 fontSize = nominal font size.
  1556.                 percent = percent change in size.
  1557.                 laser = true if laserwriter.
  1558.     
  1559.     Exit:        function result = scaled font size.
  1560.                 
  1561.     The nominal font size is multiplied by the percentage,
  1562.     then truncated.  For non-laserwriters it is then rounded down
  1563.     to the nearest font size which is available in that true size,
  1564.     without font manager scaling, or which can be generated by doubling
  1565.     an existing font size.
  1566. _____________________________________________________________________*/
  1567.  
  1568.  
  1569. short utl_ScaleFontSize (short fontNum, short fontSize, short percent,
  1570.     Boolean laser)
  1571.     
  1572. {
  1573.     short            nSize;                /* new size */
  1574.     short            x;                        /* new test size */
  1575.     
  1576.     nSize = fontSize * percent / 100;
  1577.     if (!laser) {
  1578.         x = nSize;
  1579.         while (x > 0) {
  1580.             if (RealFont(fontNum, x)) break;
  1581.             if (!(x&1) && RealFont(fontNum, x>>1)) break;
  1582.             x--;
  1583.         };
  1584.         if (x) nSize = x;
  1585.     };
  1586.     return nSize;
  1587. }
  1588.  
  1589. /*______________________________________________________________________
  1590.  
  1591.     utl_SpinCursor - Animate cursor.
  1592.     
  1593.     After calling InitSpinCursor to initialize an animated cursor, call
  1594.     SpinCursor periodically to make the cursor animate.
  1595. _____________________________________________________________________*/
  1596.  
  1597.  
  1598. void utl_SpinCursor (void)
  1599.  
  1600. {
  1601.     CursHandle        h;                /* handle to cursor */
  1602.     long                ticksNow;    /* current tick count */
  1603.     
  1604.     ticksNow = TickCount();
  1605.     if (ticksNow < LastTick + TickInterval) return;
  1606.     LastTick = ticksNow;
  1607.     CurCurs++;
  1608.     if (CurCurs >= NumCurs) CurCurs = 0;
  1609.     h = CursArray[CurCurs];
  1610.     SetCursor(*h);
  1611. }
  1612.  
  1613. /*______________________________________________________________________
  1614.  
  1615.     utl_StaggerWindow - Stagger a New Window
  1616.     
  1617.     Entry:    windRect = window portrect.
  1618.                 initialOffset = intitial pixel offset of window from corner.
  1619.                 offset = offset for subsequent staggered windows.
  1620.     
  1621.     Exit:        pos = window position.
  1622.     
  1623.     According to HIN 6, a new window should be positioned as follows:
  1624.     "The first document window should be positioned in the upper-left corner
  1625.     of the gray area of the main screen (the screen with the menu bar).
  1626.     Each additional independent window should be staggered from the upper-left
  1627.     corner of the screen that contains the largest portion of the
  1628.     frontmost window."  Also, "When a window is used or closed, its original
  1629.     position becomes available again.  The next window opened should use this
  1630.     position.  Similarly, if a window is moved onto a previously available
  1631.     position, that position becomes unavailable again."
  1632.     
  1633.     This routine implements these rules.  A position is considered to be
  1634.     "unavailable" if some other window is within (offset+1)/2 pixels of the
  1635.     position in both the vertical and horizontal directions.
  1636.     
  1637.     If all slots are occupied, the routine attempts to locate the first one
  1638.     which is occupied by only one other window.  If this attempt fails, it
  1639.     tries to locate one which is occupied by only two other windows, etc.
  1640.     Thus, if the screen is filled with staggered windows, subsequent windows
  1641.     will be staggered on top of the existing ones, forming a second "layer."
  1642.     If this layer fills up, a third layer is started, etc.
  1643. _____________________________________________________________________*/
  1644.  
  1645.  
  1646. void utl_StaggerWindow (Rect *windRect, short initialOffset, short offset, 
  1647.     Point *pos)
  1648.  
  1649. {
  1650.     GrafPtr            savedPort;        /* saved grafport */
  1651.     short                offsetDiv2;        /* offset/2 */
  1652.     short                windHeight;        /* window height */
  1653.     short                windWidth;        /* window width */
  1654.     WindowPtr        frontWind;        /* pointer to front window */
  1655.     GDHandle            gd;                /* GDevice */
  1656.     Rect                screenRect;        /* screen rectangle */
  1657.     Rect                junkRect;        /* window rectangle */
  1658.     Boolean            hasMB;            /* true if screen has menu bar */
  1659.     Point                initPos;            /* initial staggered window position */
  1660.     Point                curPos;            /* current staggered window position */
  1661.     WindowPtr        curWind;            /* pointer to current window */
  1662.     Point                windPos;            /* current window position */
  1663.     short                deltaH;            /* horizontal distance */
  1664.     short                deltaV;            /* vertical distance */
  1665.     short                layer;            /* layer number */
  1666.     short                nOccupied;        /* number windows occupying cur pos */
  1667.     Boolean            noPos;            /* true if no position is on screen */
  1668.     
  1669.     GetPort(&savedPort);
  1670.     offsetDiv2 = (offset+1)>>1;
  1671.     windHeight = windRect->bottom - windRect->top;
  1672.     windWidth = windRect->right - windRect->left;
  1673.     frontWind = FrontWindow();
  1674.     if (frontWind) {
  1675.         utl_GetWindGD(frontWind, &gd, &screenRect, &junkRect, &hasMB);
  1676.     } else {
  1677.         screenRect = screenBits.bounds;
  1678.         hasMB = true;
  1679.     };
  1680.     if (hasMB) screenRect.top += utl_GetMBarHeight();
  1681.     SetPt(&initPos, screenRect.left + initialOffset + 1, 
  1682.         screenRect.top + initialOffset + titleBarHeight);
  1683.     layer = 1;
  1684.     while (true) {
  1685.         /* Test each layer number "layer", starting with 1 and incrementing by 1.
  1686.             Break out of the loop when we find a position curPos which
  1687.             is "occupied" by fewer than "layer" other windows. */
  1688.         curPos = initPos;
  1689.         noPos = true;
  1690.         while (true) {
  1691.             /* Test each possible position curPos.  Break out of the loop
  1692.                 when we have exhaused the possible positions, or when we
  1693.                 have located one which has < layer "occupants". */
  1694.             curWind = frontWind;
  1695.             if (curPos.v + windHeight >= screenRect.bottom ||
  1696.                 curPos.h + windWidth >= screenRect.right) {
  1697.                 break;
  1698.             };
  1699.             noPos = false;
  1700.             nOccupied = 0;
  1701.             while (curWind) {
  1702.                 /* Scan the window list and count up how many of them "occupy"
  1703.                     the current location curPos.  Break out of the loop when
  1704.                     we reach the end of the list, or when the count is >=
  1705.                     layer. */
  1706.                 SetPt(&windPos, 0, 0);
  1707.                 SetPort(curWind);
  1708.                 LocalToGlobal(&windPos);
  1709.                 deltaH = curPos.h - windPos.h;
  1710.                 deltaV = curPos.v - windPos.v;
  1711.                 if (deltaH < 0) deltaH = -deltaH;
  1712.                 if (deltaV < 0) deltaV = -deltaV;
  1713.                 if (deltaH <= offsetDiv2 && deltaV <= offsetDiv2) {
  1714.                     nOccupied++;
  1715.                     if (nOccupied >= layer) break;
  1716.                 };
  1717.                 curWind = (WindowPtr)(((WindowPeek)curWind)->nextWindow);
  1718.             };
  1719.             if (!curWind) break;
  1720.             curPos.h += offset;
  1721.             curPos.v += offset;
  1722.         };
  1723.         if (!curWind || noPos) break;
  1724.         layer++;
  1725.     };
  1726.     SetPort(savedPort);
  1727.     *pos = curPos;
  1728. }
  1729.  
  1730. /*______________________________________________________________________
  1731.  
  1732.     CancelFilter - Command Period Dialog Filter Proc.
  1733.     
  1734.     Entry:    theDialog = pointer to dialog record.
  1735.                 theEvent = pointer to event record.
  1736.     
  1737.     Exit:        if command-period or escape typed:
  1738.     
  1739.                 function result = true.
  1740.                 itemHit = item number of cancel button.
  1741.                 
  1742.                 else if user specified a filterProc on the call to
  1743.                 utl_StopAlert:
  1744.                 
  1745.                 user's filterProc called, function result and itemHit
  1746.                 returned by user's filterProc.
  1747.                 
  1748.                 else:
  1749.                 
  1750.                 function result = false.
  1751.                 itemHit = undefined.
  1752. _____________________________________________________________________*/
  1753.  
  1754.  
  1755. static pascal Boolean CancelFilter (DialogPtr theDialog,
  1756.     EventRecord *theEvent, short *itemHit)
  1757.     
  1758. {
  1759.     short                key;            /* ascii code of key pressed */
  1760.     
  1761.     if (theEvent->what != keyDown && theEvent->what != autoKey) {
  1762.         if (Filter) {
  1763.             return (*Filter)(theDialog, theEvent, itemHit);
  1764.         } else {
  1765.             return false;
  1766.         };
  1767.     } else {
  1768.         key = theEvent->message & charCodeMask;
  1769.         if (key == escapeKey) {
  1770.             *itemHit = CancelItem;
  1771.             utl_FlashButton(theDialog, CancelItem);
  1772.             return true;
  1773.         } else if ((theEvent->modifiers & cmdKey) &&
  1774.             (key == '.')) {
  1775.             *itemHit = CancelItem;
  1776.             utl_FlashButton(theDialog, CancelItem);
  1777.             return true;
  1778.         } else if (Filter) {
  1779.             return (*Filter)(theDialog, theEvent, itemHit);
  1780.         } else if (key == returnKey || key==enterKey) {
  1781.             *itemHit = 1;
  1782.             utl_FlashButton(theDialog, 1);
  1783.             return true;
  1784.         } else {
  1785.             return false;
  1786.         };
  1787.     };
  1788. }
  1789.  
  1790. /*______________________________________________________________________
  1791.  
  1792.     utl_StopAlert - Present Stop Alert
  1793.     
  1794.     Entry:    alertID = resource id of alert.
  1795.                 filterProc = pointer to filter proc.
  1796.                 cancelItem = item number of cancel button, or 0 if
  1797.                     none.
  1798.     
  1799.     Exit:        function result = item number
  1800.                 
  1801.     This routine is identical to the Dialog Manager routine StopAlert,
  1802.     except that it centers the alert on the main window, and if requested,
  1803.     command/period or the Escape key is treated the same as a click on the 
  1804.     Cancel button.
  1805. _____________________________________________________________________*/
  1806.  
  1807.  
  1808. short utl_StopAlert (short alertID, ModalFilterProcPtr filterProc,
  1809.     short cancelItem)
  1810.  
  1811. {
  1812.     Handle            h;                /* handle to alert resource */
  1813.     short                result;        /* function result */
  1814.     
  1815.     h = GetResource('ALRT', alertID);
  1816.     HLock(h);
  1817.     utl_CenterDlogRect(*(Rect**)h, false);
  1818.     if (cancelItem) {
  1819.         Filter = filterProc;
  1820.         CancelItem = cancelItem;
  1821.         result = StopAlert(alertID, (ProcPtr)CancelFilter);
  1822.     } else {
  1823.         result = StopAlert(alertID, filterProc);
  1824.     };
  1825.     HUnlock(h);
  1826.     return result;
  1827. }
  1828.  
  1829. /*______________________________________________________________________
  1830.  
  1831.     utl_SysHasNotMgr - Check to See if System has Notification Manager.
  1832.     
  1833.     Exit:            function result = true if system has Notification
  1834.                         Manager..
  1835.     
  1836.     The system version must be >= 6.0.
  1837. _____________________________________________________________________*/
  1838.  
  1839.  
  1840. Boolean utl_SysHasNotMgr (void)
  1841.  
  1842. {
  1843.     if (!GotSysEnviron) GetSysEnvirons();
  1844.     return TheWorld.systemVersion >= 0x0600;
  1845. }
  1846.  
  1847. /*______________________________________________________________________
  1848.  
  1849.     utl_SysHasPopUp - Check to See if System has Popup Menus.
  1850.     
  1851.     Exit:            function result = true if system has popup menus.
  1852.     
  1853.     The system version must be >= 4.1, the machine must have the 128 ROM or 
  1854.     later, and the PopUpMenuSelect trap must exist.
  1855. _____________________________________________________________________*/
  1856.  
  1857.  
  1858. Boolean utl_SysHasPopUp (void)
  1859.  
  1860. {
  1861.     if (!GotSysEnviron) GetSysEnvirons();
  1862.     if ((TheWorld.systemVersion < 0x0410) || (TheWorld.machineType == envMac)) {
  1863.         return false;
  1864.     } else {
  1865.         return NGetTrapAddress(_PopUpMenuSelect & 0x3ff, ToolTrap) !=
  1866.             NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap);
  1867.     };
  1868. }
  1869.  
  1870. /*______________________________________________________________________
  1871.  
  1872.     utl_VolIsMFS - Test for MFS volume.
  1873.     
  1874.     Entry:    vRefNum = volume reference number.
  1875.     
  1876.     Exit:        function result = true if volume is MFS.
  1877. _____________________________________________________________________*/
  1878.  
  1879.  
  1880. Boolean utl_VolIsMFS (short vRefNum)
  1881.  
  1882. {
  1883.     HParamBlockRec        vBlock;            /* vol info param block */
  1884.     
  1885.     vBlock.volumeParam.ioNamePtr = nil;
  1886.     vBlock.volumeParam.ioVolIndex = 0;
  1887.     vBlock.volumeParam.ioVRefNum = vRefNum;
  1888.     vBlock.volumeParam.ioVSigWord = 0xd2d7;    /* in case we don't have HFS */
  1889.     (void) PBHGetVInfo(&vBlock, false);
  1890.     return vBlock.volumeParam.ioVSigWord == 0xd2d7;
  1891. }
  1892.  
  1893. /*______________________________________________________________________
  1894.  
  1895.     utl_WaitNextEvent - Get Next Event.
  1896.     
  1897.     Entry:    eventMask = event mask.
  1898.                 sleep = sleep interval.
  1899.                 mouseRgn = mouse region.
  1900.     
  1901.     Exit:        theEvent = the next event.
  1902.                 function result = true if event to be processed.
  1903.                 
  1904.     This routine calls WaitNextEvent if the trap exists, otherwise it
  1905.     calls GetNextEvent.  
  1906.     
  1907.     If GetNextEvent is called, the sleep and mouseRgn parameters are 
  1908.     ignored.  SystemTask is also called.
  1909.                 
  1910.     This routine also saves and restores the current grafport.  This is
  1911.     necessary to protect against some GetNextEvent trap patches which change
  1912.     the grafport without restoring it (e.g., the Flex screen saver).
  1913. _____________________________________________________________________*/
  1914.  
  1915.  
  1916. Boolean utl_WaitNextEvent (short eventMask, EventRecord *theEvent,
  1917.     long sleep, RgnHandle mouseRgn)
  1918.  
  1919. {
  1920.     GrafPtr            curPort;        /* pointer to current grafport */
  1921.     Boolean            result;        /* function result */
  1922.     static short    wne = 2;        /* 0 if WaitNextEvent does not exist
  1923.                                             1 if WaitNextEvent exists
  1924.                                             2 if we don't yet know (first call) */
  1925.     
  1926.     /* Find out whether the WaitNextEvent trap is implemented if this is 
  1927.         the first call. */
  1928.     
  1929.     if (wne == 2) {
  1930.         if (!GotSysEnviron) GetSysEnvirons();
  1931.         if (TheWorld.machineType < 0) {
  1932.             wne = false;
  1933.         } else {
  1934.             wne = (NGetTrapAddress(_WaitNextEvent & 0x3ff, ToolTrap) == 
  1935.                 NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap)) ? 0 : 1;
  1936.         };
  1937.     };
  1938.     
  1939.     /* Save the port, call the trap, and restore the port. */
  1940.     
  1941.     GetPort(&curPort);
  1942.     if (wne) {
  1943.         result = WaitNextEvent(eventMask, theEvent, sleep, mouseRgn);
  1944.     } else {
  1945.         SystemTask();
  1946.         result = GetNextEvent(eventMask, theEvent);
  1947.     };
  1948.     SetPort(curPort);
  1949.     return result;
  1950. }
  1951.